{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8ad16f18",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b9d4226a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构建ndarray数据结构\n",
    "nd1 = np.array([[1,2,3],[4,5,6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "77b918ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "print(nd1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "51b27c40",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "source": [
    "print(type(nd1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e21541d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "nd2 = np.array([['1','2','3'],['4','5','6']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c05a5cf5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['1' '2' '3']\n",
      " ['4' '5' '6']]\n"
     ]
    }
   ],
   "source": [
    "print(nd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b3b8af27",
   "metadata": {},
   "outputs": [],
   "source": [
    "newNd2 = nd2.astype('int')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b0883f21",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "print(newNd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "933111ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "newNd2.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4af9ba7f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "newNd2.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "2311e2d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.5"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "newNd2.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "36058e93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.        , 0.69314718, 1.09861229],\n",
       "       [1.38629436, 1.60943791, 1.79175947]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.log(newNd2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "b0bcedf3",
   "metadata": {},
   "outputs": [],
   "source": [
    "list2 = [\n",
    "    [0,1,2,3,4,5],\n",
    "     [10,11,12,13,14,15],\n",
    "     [20,21,22,23,24,25],\n",
    "     [30,31,32,33,34,35],\n",
    "     [40,41,42,43,44,45],\n",
    "     [50,51,52,53,54,55],\n",
    "]\n",
    "nd3 = np.array(list2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "21d7d697",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [10 11 12 13 14 15]\n",
      " [20 21 22 23 24 25]\n",
      " [30 31 32 33 34 35]\n",
      " [40 41 42 43 44 45]\n",
      " [50 51 52 53 54 55]]\n"
     ]
    }
   ],
   "source": [
    "print(nd3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "adef113a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[20, 21, 22, 23, 24, 25],\n",
       "       [30, 31, 32, 33, 34, 35]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd3[2:4:1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "634c1a93",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[23, 24],\n",
       "       [33, 34]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# nd3[行的切片,列的切片]\n",
    "nd3[2:4:1,3:5:1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "0681d19b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5, 14, 23, 32, 41])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd3[(0,1,2,3,4),(5,4,3,2,1)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "70d77892",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([34, 35, 40, 41, 42, 43, 44, 45, 50, 51, 52, 53, 54, 55])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd3[nd3>33]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "6d4dd0d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  5, 11, 13, 15, 21, 23, 25, 31, 33, 35, 41, 43, 45, 51, 53,\n",
       "       55])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd3[nd3%2 == 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "1e48199d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 矩阵运算\n",
    "# + - * / 对位运算\n",
    "nd4 = np.array([[1,2,3],[4,5,6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "241f1e69",
   "metadata": {},
   "outputs": [],
   "source": [
    "nd5 = np.array([[2,3,4],[5,6,7]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "213a1026",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3,  5,  7],\n",
       "       [ 9, 11, 13]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd4 + nd5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "ceef014a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1, -1, -1],\n",
       "       [-1, -1, -1]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd4 - nd5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "5f948b16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  6, 12],\n",
       "       [20, 30, 42]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd4 * nd5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "d2dcbcb7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.5       , 0.66666667, 0.75      ],\n",
       "       [0.8       , 0.83333333, 0.85714286]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nd4 / nd5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "796a0454",
   "metadata": {},
   "outputs": [],
   "source": [
    "nd6 = np.array([[1,2],[3,4],[5,6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "5752cf10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "source": [
    "print(nd4)\n",
    "print(nd6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "730b4d7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[22, 28],\n",
       "       [49, 64]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(nd4,nd6) # 矩阵的乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "5f7a679f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 0.5       ],\n",
       "       [0.33333333, 0.25      ],\n",
       "       [0.2       , 0.16666667]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1/nd6"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "725973a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2.26666667, 1.5       ],\n",
       "       [6.86666667, 4.25      ]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(nd4,1/nd6) # 矩阵的除法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b02b1f44",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
